<?php
declare (strict_types=1);

namespace app\exam\service\admin;

use app\common\service\BaseService;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use app\exam\logic\admin\ModulePoint as ModulePointLogic;
use think\facade\Request;

/**
 * Class ModuleConfig
 *
 * @package app\exam\service\admin
 */
class ModulePoint
{

    /**
     * @Inject()
     *
     * @var ModulePointLogic
     */
    protected $modulePointLogic;

    /**
     * 知识点配置
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/03/15
     *
     * @param int $moduleId
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function config(int $moduleId)
    {
        $data = $this->modulePointLogic->getTreeList($moduleId);

        // 定义字段
        $keyList = [
            'title'          => ListItem::make()->title('知识点名称'),
            'cates'       => ListItem::make()->title('所属分类'),
            // 'point_limit'    => ListItem::make()->title('知识点数量限制'),
            // 'question_limit' => ListItem::make()->title('出题数量限制'),
        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 新建专项
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/31
     *
     * @return Response
     */
    public function create(int $moduleId)
    {
        $data = Request::post();
        $this->modulePointLogic->createPoint($moduleId, $data);

        return BaseService::parseToData([], 1, '保存成功');
    }

    /**
     * 编辑
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2022-03-15
     * @param  int    $moduleId  [description]
     * @param  int    $pointId [description]
     * @return [type]            [description]
     */
    public function edit()
    {
        $data = Request::put();
        $this->modulePointLogic->editPoint($data);

        return BaseService::parseToData([], 1, '保存成功');
    }

    /**
     * 删除
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2022-03-15
     * @param  int    $pointId [description]
     * @return [type]            [description]
     */
    public function delete(int $pointId)
    {
        $this->modulePointLogic->deletePoint($pointId);

        return BaseService::parseToData([], 1, '删除成功');
    }

}